package feng.demo.controller.adm;

import cn.dev33.satoken.secure.SaBase64Util;
import feng.demo.dao.LargefileDao;
import feng.demo.entity.Largefile;
import feng.demo.entity.User;
import feng.demo.entity.XJson;
import feng.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adm")
public class AdmController {


    @Autowired
    UserService userService;
    @Autowired
    LargefileDao largefileDao;

    @GetMapping("/findAll/{id}/{p}/{l}")
    public String findAll(@PathVariable("id")Integer permid,@PathVariable("p")Integer p,@PathVariable("l")Integer l){
//           查找用户
        Map<String,Object> map=new HashMap<>();
        List<User> list = userService.findByPermidLimit(permid, p, l);
        int count = userService.findCount(permid);
        map.put("user",list);
        map.put("count",count);
        return new XJson(200,"",map).toString();
    }
    @GetMapping("/del/{uid}/{id}/{p}/{l}")
    public String del(@PathVariable("uid")Integer uid,@PathVariable("id")Integer permid,@PathVariable("p")Integer p,@PathVariable("l")Integer l){
//          冻结用户
        User user = userService.findByUid(uid);
        User u = new User();
        u.setUid(uid);
        u.setClosed(1);
        int update = userService.update(u);
        Map<String,Object> map=new HashMap<>();
        List<User> list = userService.findByPermidLimit(permid, p, l);
        int count = userService.findCount(permid);
        map.put("user",list);
        map.put("count",count);
        return new XJson(200,"冻结成功",map).toString();
    }
    @GetMapping("/dels/{uid}/{id}/{p}/{l}")
    public String dels(@PathVariable("uid")Integer uid,@PathVariable("id")Integer permid,@PathVariable("p")Integer p,@PathVariable("l")Integer l){
//          解除冻结用户
        User user = userService.findByUid(uid);
        User u = new User();
        u.setUid(uid);
        u.setClosed(0);
        int update = userService.update(u);
        Map<String,Object> map=new HashMap<>();
        List<User> list = userService.findByPermidLimit(permid, p, l);
        int count = userService.findCount(permid);
        map.put("user",list);
        map.put("count",count);
        return new XJson(200,"成功解除冻结",map).toString();
    }
    @GetMapping("/findByUid/{uid}")
    public String findByUid(@PathVariable("uid")Integer uid){
        User user = userService.findByUid(uid);

        return new XJson(200,"",user).toString();
    }
    @PostMapping("/update")
    public String update(User user, Integer permid, MultipartFile img){
        user.getPerm().setPermid(permid);
        String pwd = SaBase64Util.decode(user.getPassword());
        user.setPassword(pwd);
        if (img==null){
            userService.update(user);
        }else {
            largefileDao.delete(user.getPic());
            Largefile largefile = new Largefile();
            try {
                largefile.setFilename(img.getOriginalFilename());
                byte[] bytes=img.getBytes();
                largefile.setContent(bytes);
            }catch (Exception e){

            }
            Integer integer = largefileDao.add(largefile);
            user.setPic(largefile.getId());
            userService.update(user);
        }
        return new XJson(200,"更新成功","").toString();

    }



}
